<template>
  <div class="login">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h2 class="title">欢迎登录</h2>
      <el-form-item prop="username">
        <el-input
          v-model.trim="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="用户名/手机号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model.trim="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="confirmLoading"
          :disabled="confirmLoading"
          type="primary"
          class="btn-loading"
          style="width: 100%"
          @click.prevent="handleLogin"
        >
          <span v-if="!confirmLoading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import userApi from '@/api/user/index'
import LsObj from '@/common/core/ls/index'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

const loginForm = reactive({
  username: "",
  password: "",
});
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }],
};
const confirmLoading = ref(false);
const loginFormRef = ref();

function handleLogin() {
  loginFormRef.value.validate(async (valid) => {
    if (!valid) {
      return;
    }
    confirmLoading.value = true;
    const { result } = await userApi.login(loginForm)
    if (result) {
      LsObj.common.token.set(result.data.token)
      if (route.query.from) {
        router.push(route.query.from)
      } else {
        router.push('/')
      }
    }
  });
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
.title {
  text-align: center;
  font-size: 32px;
  padding: 30px 0 20px 0;
  color: #111111;
}
.login-form {
  width: 300px;
  height: 460px;
  padding: 30px 100px 5px 100px;
  box-shadow: 0px 5px 10px 0px var(--el-color-warning);
  border-radius: 10px;
  border: solid 1px #ffffff;
  .el-input {
    height: 44px;
    line-height: 44px;
    background-color: #ffffff;
    border-radius: 4px;
    border-color: #d9ddeb;
    input {
      height: 44px;
    }
    :deep(.el-input__inner) {
      height: 44px;
      color: #aaaaaa;
      background-color: #ffffff !important;
      font-size: 14px;
    }
  }
  .el-input--medium {
    :deep(.el-input__inner) {
      height: 44px;
    }
  }
  .input-icon {
    height: 20px;
    width: 20px;
    margin-left: 12px;
  }
  .el-input--prefix {
    :deep(.el-input__inner) {
      padding-left: 50px;
    }
  }
  :deep(.el-input__inner:focus) {
    border-color: #fd6f03;
  }
  .btn-loading {
    height: 50px;
    box-shadow: 0px 5px 10px 0px rgba(253, 111, 3, 0.2);
    border-radius: 4px;
    font-size: 16px;
    margin-top: 10px;
  }
}
</style>
